package com.softer.chengxin.project.dao.provider;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.project.domin.Proprietor;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * @author Gong Zhiyang
 * @date 2018/10/16
 */
public class ProprietorDaoProvider {
    /**
     * 查询业主委员会人员数量
     *
     * @param map 查询实体
     * @return 数量
     */
    public String queryNum(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        Proprietor proprietor = (Proprietor) map.get("proprietor");
        sql.append("SELECT COUNT(*) from company_proprietor WHERE project_id=#{proprietor.projectId}");
        commonSql(sql, proprietor);
        return sql.toString();
    }
    /**
     * 查询业主委员会人员数量
     *
     * @param map 查询实体
     * @return 数量
     */
    public String queryList(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        Proprietor proprietor = (Proprietor) map.get("proprietor");
        Page page = (Page) map.get("page");
        sql.append("SELECT id,project_id as projectId,`name`,sex,age,education_level as educationLevelCode,(SELECT name FROM dic_info WHERE code = education_level) AS educationLevel,company, ");
        sql.append("phone,business_address as businessAddress,address, duty AS dutyCode,(SELECT name FROM dic_info WHERE code = duty) AS duty,political_status as politicalStatus,");
        sql.append("state,housing_area as housingArea,remark from company_proprietor WHERE project_id=#{proprietor.projectId}");
        commonSql(sql, proprietor);
        sql.append(" LIMIT ");
        sql.append(page.getBeginIndex()).append(",").append(page.getLimit());
        return sql.toString();
    }

    private void commonSql(StringBuffer sql, Proprietor proprietor) {
        if (StringUtils.isNotBlank(proprietor.getName())) {
            sql.append(" and instr(name,#{proprietor.name})>0");
        }
        if (StringUtils.isNotBlank(proprietor.getDuty())) {
            sql.append(" and duty=#{proprietor.duty}");
        }
        if (StringUtils.isNotBlank(proprietor.getSex())) {
            sql.append(" and sex=#{proprietor.sex}");
        }
    }
}
